package com.mms.service.impl;

import com.mms.context.Constants;
import com.mms.entity.model.JsonData;
import com.mms.entity.model.PagerTool;
import com.mms.entity.service.impl.EntityServiceImpl;
import com.mms.pojo.SysManager;
import com.mms.service.SysManagerService;
import com.mms.utils.RequestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by dangz on 2017/5/21/021.
 */
@Service
public class SysManagerServiceImpl extends EntityServiceImpl<SysManager> implements SysManagerService {
    @Override
    public JsonData getList(PagerTool pt, HttpServletRequest req) {
        SysManager manager = (SysManager) req.getAttribute(Constants.USER_IN_REQUEST);
        StringBuffer cb = new StringBuffer("from SysManager a where 1=1");
        StringBuffer lb = new StringBuffer("select new map(a.id as id,a.account as name,date_format(a.addTime,'%Y-%m-%d') as addTime,date_format(a.updateTime,'%Y-%m-%d') as updateTime,a.status as status,a.sysRole.name as role,a.loginIp as ip) from SysManager a where 1=1 ");
        if (manager != null && manager.getSysManager() != null){
            cb.append(" and a.sysManager.id="+manager.getSysManager().getId());
            lb.append(" and a.sysManager.id="+manager.getSysManager().getId());
        }
        Map<String, Object> params = RequestUtils.getQueryParams(req);;
        if (params.get("account") != null && params.get("account") != "" ){
            cb.append("  and a.account like '%"+params.get("account")+"%'");
            lb.append("  and a.account like '%"+params.get("account")+"%'");
        }if (params.get("role") != null && params.get("role") != "" ){
            cb.append("  and a.sysRole.name like '%"+params.get("role")+"%'");
            lb.append("  and a.sysRole.name like '%"+params.get("role")+"%'");
        }
        if (params.get("pdate") != null && params.get("pdate") != "" ){
            cb.append("  and a.addTime like '%"+params.get("pdate")+"%'");
            lb.append("  and a.addTime like '%"+params.get("pdate")+"%'");
        }
        if (StringUtils.isNotEmpty(pt.getSortby())){
            lb.append("  order by "+pt.getSortby()+" "+pt.getSorttype());
        }
        JsonData jd = new JsonData();
        jd.setTotal(getCount(cb.toString()));
        jd.setRoot(find(pt.getStart(),pt.getEnd(),lb.toString()));
        return jd;
    }
}
